exports.controllers={
	admin_market_items: function(space) {
		if (!space.roles.marketconfig) return "-";
		var mode=space.fields.mode||"list";
		switch (mode) {
			case "list":
				space.rows=[];
				space.mfact.list_hier("market_category",{order_by: "main.ordering"}).forEach(function(cat) {
					space.rows.push({
						rowClass: "category",
						id:"cat-"+cat.id,
						level:cat.level-1,
						cells:[cat.name,"+"]
					});
					space.mfact.list_hier("market_item",{category_id: cat.id,order_by: "main.name,main.id"}).forEach(function(item) {
						space.rows.push({
							rowClass: "item",
							id: item.id,
							level:cat.level+item.level-1,
							cells:[
								item.name,
								"+",
								item.vendor_id,
								item.price,
								item.enabled,
								item.in_stock
							]
						});
					});
				});
				space.vendors=space.mfact.list("market_vendor",{});
				return space.views.process("widgets/market_items/admin_"+mode,space);
				break;
			case "edit":
				space.item={};
				space.currencies=space.site.sql.execute_and_fetch("currencies/list");
//				space.categories=space.mfact.list("market_category",{order_by: "ordering",hier: true});
				space.vendors=space.site.sql.execute_and_fetch("market_vendors/list_fk");
				if (space.fields.id) space.item=space.mfact.get("market_item",space.fields.id);
				if (space.fields.category_id) space.item.category_id=space.fields.category_id.match(/(\d+)/)[1];
				if (space.fields.parent_id) {
					space.parent=space.mfact.get("market_item",space.fields.parent_id);
					space.item.category_id=space.parent.category_id;
				} else if (space.item.parent_id) {
					space.parent=space.mfact.get("market_item",space.item.parent_id);
				}
				space.has_parent=space.parent?true:false;
				space.param_groups=space.mfact.list("market_category_param_group",{order_by: "main.ordering",category_id:space.item.category_id});
				space.param_groups.forEach(function(grp) {
					grp.params=space.site.sql.execute_and_fetch("market_item_param_values/list_for_admin_panel",{param_group_id:grp.id,item_id:space.fields.id});
					grp.params.forEach(function(p) {
						p.value=(p.datatype_id==1)?p.value_number:p.value_text;
						p.values=[];
						space.site.sql.execute_and_fetch("market_item_param_values/list_for_admin_available_"+(p.datatype_id==1?"number":"text"),{param_id:p.id}).forEach(function(x){
							p.values.push(x.value);
						});
					});
				});
				space.columns=[];
				var colnames=[
					"name",
					"name_robots",
					["label","category_name","category_name"],
					["select","vendor_id","vendors","vendor_name"],
					["file_group_id","image_group_id",null,null,true],
					["textareaedit","abstract"],
					["textareaedit","description"],
					["textareaedit","description_robots"],
					"seo_keywords",
					"seo_title",
					"seo_subject",
					"seo_description",
					["checkbox","enabled"],
					["checkbox","in_stock"],
					["checkbox","is_new"],
					["checkbox","is_recomended"],
					["checkbox","is_discount"],
					["checkbox","images_replace"],
					"price",
					["select","currency_id","currencies"],
					"fkid"
				];
				for (var i=0,s=colnames.length;i<s;i++) {
					var colinfo=colnames[i];
					if (typeof colinfo=="string") colinfo=["textedit",colinfo];
					var h={
						type: colinfo[0],
						name: colinfo[1],
						width: 400,
						label: space.functions.L(space,"widgets/market_items/admin_edit","col_"+colinfo[1]),
						value: space.item[colinfo[1]]
					};
					if (space.parent && !colinfo[4]) {
						h.disableable=true;
						h.disabled=!space.item[colinfo[1]];
					}
					var hp={
						type: "label",
						name: "parent_"+colinfo[1],
						width: 400,
						label: h.label,
						value: space.parent?space.parent[colinfo[1]]:undefined
					};
					if (colinfo[0]=="select") h.values=space[colinfo[2]];
					if (colinfo[0]=="textareaedit") {h.wysiwyg=true;hp.html=true;}
					if (colinfo[0]=="file_group_id") {
						if (h.value) h.value=space.functions.ajajform_file_group_value(space,h.value); else h.value={};
					}
					//if (space.parent) space.columns.push(hp);
					space.columns.push(h);
				}
				return space.views.process("widgets/market_items/admin_"+mode,space);
				break;
			case "save":
				var parent;
				if (space.fields.parent_id) parent=space.mfact.get("market_item",space.fields.parent_id);
				var item;
				if (space.fields.id) {
					item=space.mfact.get("market_item",space.fields.id);
					item.UpdateFromObject(space.fields);
				} else {
					item=space.mfact.from_object("market_item",space.fields);
				}
				if (parent) {
					if (!item.vendor_id) item.vendor_id=parent.vendor_id;
				}
				item.MakeZeroes(["in_stock","is_recomended","is_discount","is_new","images_replace"]);
				item.Save();
				for (var k in space.fields) {
					var arr;
					arr=k.match(/^param_(\d+)$/);
					if (!arr) continue;
					var iparam={
						item_id: item.id,
						value: space.fields[k],
						param_id: arr[1]
					};
					var i0=space.site.sql.execute_and_fetch_one("market_category_params/get",iparam);
					if (i0.datatype_id==1) iparam.value_number=iparam.value; else iparam.value_text=iparam.value;
					var i1=space.site.sql.execute_and_fetch_one("market_item_param_values/get_by_item_and_param",iparam);
					if (i1) {
						iparam.param_value_id=i1.id;
						space.site.sql.execute("market_item_param_values/update",iparam);
					} else {
						space.site.sql.execute("market_item_param_values/insert",iparam);
					}
				}
				return JSON.stringify({redirect:{url: space.action}});
				break;
			case "delete":
				space.item=space.mfact.get("market_item",space.fields.id);
				return space.views.process("widgets/market_items/admin_"+mode,space);
				break;
			case "delete_save":
				space.mfact.get("market_item",space.fields.id).Delete();
				return JSON.stringify({redirect:{url: space.action}});
				break;
			case "reorder":
				space.functions.reorder_grids(space,"_models/market_item/");
				return JSON.stringify({});
				break;
		} 
	}
};

